using System;
using System.Collections.Generic;
using System.Text;
using MS.Core.Base.Data;
using System.Data;
using MS.Core.Constants;
using LHS.Core.CaseRecord.Classes;
using System.Data.Common;
using System.Collections.Specialized;
using LHS.Core.TableInfo;

namespace LHS.Core.CaseRecord.DataCommons
{
    public class CommonDynamicTable : DBBaseData
    {
        public DataTable getDataDynamicTableDT(String strTableName, String strPatientID, String strOtherID1, String strOtherID2, String strOtherID3)
        {
            DataTable dtReturn;
            try
            {
                String strSQL = "SELECT *, " + Convert.ToInt32(GlobalEnumType.Status.HAVE_VALUE).ToString() + " AS " + DBBaseInfo.Hidden_Status + " FROM " + strTableName;
                Boolean haveWhere = false;

                if (strPatientID != "")
                {
                    strSQL += " WHERE " + clsDynamicTable.PatientID + " = '" + strPatientID + "'";
                    haveWhere = true;
                }

                if (strOtherID1 != "")
                {
                    if (haveWhere)
                    {
                        strSQL += " AND ";
                    }
                    else
                    {
                        strSQL += " WHERE ";
                        haveWhere = true;
                    }
                    strSQL += clsDynamicTable.OtherID1 + " = '" + strOtherID1 + "'";
                }

                if (strOtherID2 != "")
                {
                    if (haveWhere)
                    {
                        strSQL += " AND ";
                    }
                    else
                    {
                        strSQL += " WHERE ";
                        haveWhere = true;
                    }
                    strSQL += clsDynamicTable.OtherID2 + " = '" + strOtherID2 + "'";
                }

                if (strOtherID3 != "")
                {
                    if (haveWhere)
                    {
                        strSQL += " AND ";
                    }
                    else
                    {
                        strSQL += " WHERE ";
                        haveWhere = true;
                    }
                    strSQL += clsDynamicTable.OtherID3 + " = '" + strOtherID3 + "'";
                }

                DbCommand cmd = base.GetDbCommand(CommandType.Text, strSQL);
                dtReturn = base.GetData(cmd, strTableName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dtReturn;
        }

        public Boolean isExistsTable(String strTableName)
        {
            Boolean blnReturnValue = false;
            try
            {
                DbCommand cmd = base.GetDbCommand(CommandType.Text, clsDynamicTable.getSQLCheckTableIsExists(strTableName));
                DataTable dtReturn = base.GetData(cmd, strTableName);
                if (dtReturn.Rows.Count > 0)
                    blnReturnValue = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return blnReturnValue;
        }

        public int createTable(DataTable dtCaseRecord_Leaf, String strTableName)
        {
            try
            {
                DbCommand cmd = base.GetDbCommand(CommandType.Text, clsDynamicTable.getSQLCreateTable(dtCaseRecord_Leaf, strTableName));
                return base.LoadFromSqlNoExec(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int alterTable(DataTable dtCaseRecord_Leaf, String strTableName, DataTable dtCaseRecord_Tree)
        {
            try
            {
                DbCommand cmd = base.GetDbCommand(CommandType.Text, clsDynamicTable.getSQLAlterTable(dtCaseRecord_Leaf, strTableName, dtCaseRecord_Tree));
                return base.LoadFromSqlNoExec(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int deleteTable(String strTableName)
        {
            try
            {
                DbCommand cmd = base.GetDbCommand(CommandType.Text, clsDynamicTable.getSQLDeleteTable(strTableName));
                return base.LoadFromSqlNoExec(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable getStatisticalDataDT(String strSQL, String strTableName, ListDictionary paramWhere)
        {
            DataTable dtReturn;
            try
            {
                DbCommand cmd = base.GetDbCommand(CommandType.Text, strSQL, paramWhere);
                dtReturn = base.GetData(cmd, strTableName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dtReturn;
        }

        public DataTable getPatientDT(String strPatientID)
        {
            DataTable dtReturn;
            try
            {
                String strSQL = "SELECT *, " + Convert.ToInt32(GlobalEnumType.Status.HAVE_VALUE).ToString() + " AS " + DBBaseInfo.Hidden_Status + " FROM " + InfoPatients.TABLENAME + " WHERE " + InfoPatients.PATISN + " = '" + strPatientID + "'";
                DbCommand cmd = base.GetDbCommand(CommandType.Text, strSQL);
                dtReturn = base.GetData(cmd, InfoPatients.TABLENAME);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dtReturn;
        }
    }
}
